﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.WebInfo;

namespace com.subb.WebInformation
{
    public class WI_ExpertsDAL
    {
        public List<WI_Experts> GetAllExperts(int cataid, int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                IQueryable<WI_Experts> query = null;
                query = ctx.WI_Experts;
                if (cataid != 0)
                {
                    query = query.Where(c => c.ExpertCataID == cataid);
                }
                query = query.OrderBy(c => c.SortNum);
                ttlCount = query.Count();
                List<WI_Experts> list = query.Skip(start).Take(limit).ToList();
                return list;
            }

        }
        /// <summary>
        /// 专家动态，随机获取专家列表
        /// </summary>
        /// <param name="limit"></param>
        /// <returns></returns>
        public List<WI_Experts> GetRandomExperts(int limit)
        {
            WebsiteDbEntities ctx = new WebsiteDbEntities();
            List<WI_Experts> list = ctx.WI_Experts.OrderBy(c => Guid.NewGuid()).Take(limit).ToList();
            return list;
        }

        public List<WI_Experts> GetAllExpertsByView(int cataid, int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            WebsiteDbEntities ctx = new WebsiteDbEntities();
            IQueryable<WI_Experts> query = null;
            query = ctx.WI_Experts;
            if (cataid != 0)
            {
                query = query.Where(c => c.ExpertCataID == cataid);
            }
            query = query.Where(c=>c.IsShow==1&&c.IsDelete==0).OrderBy(c => c.SortNum);
            ttlCount = query.Count();
            List<WI_Experts> list = query.Skip(start).Take(limit).ToList();
            return list;

        }
        public WI_Experts GetExpertsByView(int id)
        {
            WebsiteDbEntities ctx = new WebsiteDbEntities();
            WI_Experts obj = ctx.WI_Experts.SingleOrDefault(c => c.ExpertID == id&&c.IsShow==1);
            return obj;

        }


        public WI_Experts GetExperts(int id)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                WI_Experts obj = ctx.WI_Experts.SingleOrDefault(c => c.ExpertID == id);
                return obj;
            }
        }

        public void CreateExperts(WI_Experts addObj)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.WI_Experts.AddObject(addObj);
                ctx.SaveChanges();
            }
        }

        public bool UpdateExperts(int id, Experts updateObj)
        {
            bool result = false;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                WI_Experts obj = ctx.WI_Experts.SingleOrDefault(c => c.ExpertID == id);
                if (obj != null)
                {
                    obj.ExpertCataID = updateObj.ExpertCataID;
                    obj.Achievement = updateObj.Achievement;
                    obj.ExpertJob = updateObj.ExpertJob;
                    obj.ExpertName = updateObj.ExpertName;
                    if (!string.IsNullOrEmpty(updateObj.ExpertPic))
                    {
                        obj.ExpertPic = updateObj.ExpertPic;
                    }
                    obj.ExpertPosition = updateObj.ExpertPosition;
                    obj.Introduction = updateObj.Introduction;
                    obj.IsShow = byte.Parse(updateObj.IsShow.ToString());
                    obj.OtherMsg = updateObj.OtherMsg;
                    obj.SkillField = updateObj.SkillField;
                    obj.SortNum = updateObj.SortNum;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveExperts(int id)
        {
            bool result = false;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                WI_Experts obj = ctx.WI_Experts.SingleOrDefault(c => c.ExpertID == id);
                if (obj != null)
                {
                    ctx.WI_Experts.DeleteObject(obj);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public List<WI_Experts> SearchByName(string key, int start, int limit, out int ttlCount)
        {
            WebsiteDbEntities ctx = new WebsiteDbEntities();
            var query = ctx.WI_Experts.Where(c => c.IsShow == 1 && c.IsDelete == 0 && c.ExpertName.Contains(key)).OrderBy(c => c.SortNum).ThenBy(c => c.ExpertID);
            ttlCount = query.Count();
            List<WI_Experts> explist = query.Skip(start).Take(limit).ToList();
            return explist;
        }
    }
}
